$(function () {
    $("#jqGrid").jqGrid({

        url: baseURL + 'sys/tableinfo/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', width: 50, key: true},
            {label: '数据源ID', name: 'dbId', index: 'db_id', width: 80},
            {label: '表名', name: 'tableName', index: 'table_name', width: 80},
            {label: '注释', name: 'remarks', index: 'remarks', width: 80},
            {label: '数据量', name: 'count', index: 'count', width: 80}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },

        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        q: {
            connid: "0"
        },
        showList: true,
        title: null,
        tableInfo: {},
        dataSelect: [],
        arra: []
    },
    created: function () {

        this.querySelect();
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.tableInfo = {};
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        querySelect: function () {
            var that = this;
            var url = "sys/databaseinfo/querySelect";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                success: function (r) {
                    if (r.code === 0) {

                        that.dataSelect = r.list;


                    } else {
                        layer.alert("查询失败！");
                    }
                    return false;
                }
            });
        },

        upCount: function () {

            var that = this;
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            var layerId = layer.msg('生成结果中，请等待！', {time: 0});
            var grid = $("#jqGrid");
            var selectedIDs = grid.getGridParam("selarrrow");
            var rowData = grid.jqGrid('getRowData', id);
            console.log(rowData, "+++++++++______________");


            var tableid = rowData.id;
            var connid = rowData.dbId;
            var tableName = rowData.tableName;
            var url = "sys/tableinfo/upCount";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                data: {
                    connid: connid,
                    tableid: tableid,
                    tableName: tableName
                },

                success: function (r) {
                    if (r.code === 0) {
                        console.log(r);
                        layer.close(layerId);
                        layer.msg("生成成功", {icon: 1});
                        vm.reload();
                    } else {
                        layer.alert("查询失败！");
                        layer.close(layerId);
                    }
                    return false;
                }
            });

        },
        upMoreCount: function () {

            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }
            var layerId = layer.msg('生成结果中，请等待！', {time: 0});
            var url = "sys/tableinfo/upMoreCount";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(ids),

                success: function (r) {
                    if (r.code === 0) {
                        console.log(r);
                        layer.close(layerId);
                        layer.msg("生成成功", {icon: 1});
                        vm.reload();
                    } else {
                        layer.alert("查询失败！");
                        layer.close(layerId);
                    }
                    return false;
                }
            });

        },
        queryCloumns: function () {

            var that = this;
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            var layerId = layer.msg('查询结果中，请等待！', {time: 0});
            var grid = $("#jqGrid");
            var selectedIDs = grid.getGridParam("selarrrow");
            var rowData = grid.jqGrid('getRowData', id);
            console.log(rowData, "+++++++++______________");

            var connid = rowData.dbId;
            var tableId = rowData.id;
            var tableName = rowData.tableName;

            var url = "sys/tableinfo/queryclomuns";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify({connid: connid, tableId:tableId,tableName: tableName}),

                success: function (r) {
                    if (r.code === 0) {
                        console.log(r);
                        layer.alert("生成成功！");

                        // $('#tableName').text("表名：" + r.columns[0]);
                        // $('#count').text("总数量：" + r.columns[1]);
                        // that.arra = r.columns[2];
                        layer.close(layerId);
                        //$("#layerTable").show();
                    } else {
                        layer.alert("生成失败！");
                        layer.close(layerId);
                    }
                    return false;
                }
            });

        },
        queryMoreCloumns: function () {

            var ids = getSelectedRows();
            console.log("--------------------------------",ids)
            if (ids == null) {
                return;
            }
            var layerId = layer.msg('批量生成列信息中，请等待！', {time: 0});
            var url = "sys/tableinfo/queryMoreClomuns";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(ids),


                success: function (r) {
                    if (r.code === 0) {
                        console.log(r);
                        layer.alert("生成成功！");

                        // $('#tableName').text("表名：" + r.columns[0]);
                        // $('#count').text("总数量：" + r.columns[1]);
                        // that.arra = r.columns[2];
                        layer.close(layerId);
                        //$("#layerTable").show();
                    } else {
                        layer.alert("生成失败！");
                        layer.close(layerId);
                    }
                    return false;
                }
            });

        },
        saveOrUpdate: function (event) {
            $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function () {
                var url = vm.tableInfo.id == null ? "sys/tableinfo/save" : "sys/tableinfo/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.tableInfo),
                    success: function (r) {
                        if (r.code === 0) {
                            layer.msg("操作成功", {icon: 1});
                            vm.reload();
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        } else {
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }
            var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定', '取消'] //按钮
            }, function () {
                if (!lock) {
                    lock = true;
                    $.ajax({
                        type: "POST",
                        url: baseURL + "sys/tableinfo/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function (r) {
                            if (r.code == 0) {
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            } else {
                                layer.alert(r.msg);
                            }
                        }
                    });
                }
            }, function () {
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "sys/tableinfo/info/" + id, function (r) {
                vm.tableInfo = r.tableInfo;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'connid': vm.q.connid},
                page: page
            }).trigger("reloadGrid");
        }
    },
    updated: function () {

        console.log(this.dataSelect, "++_______________")
    }
});